package com.hwg.shiliupro.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hwg.shiliupro.dao.MsgInfoMapper;
import com.hwg.shiliupro.po.MsgInfo;
import com.hwg.shiliupro.service.MsgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MsgInfoServiceImpl implements MsgInfoService {
    @Autowired
    private MsgInfoMapper msgInfoMapper;
    @Override
    public int insert(MsgInfo msgInfo) {
        return msgInfoMapper.insert(msgInfo);
    }

    /**
     * 通过发送者id和接收者id查询 消息MsgInfo
     *
     * @param fromUserId 从用户id
     * @param toUserId   用户id
     */
    @Override
    public List<MsgInfo> selectMsgList(Integer fromUserId, Integer toUserId) {
        QueryWrapper<MsgInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_user_id",fromUserId);
        queryWrapper.eq("to_user_id",toUserId);
        List<MsgInfo> msgInfos = msgInfoMapper.selectList(queryWrapper);
        return msgInfos;
    }

    /**
     * 修改消息里的 是否已读的字段值 改为1证明已读
     *
     * @param fromUserId 从用户id
     * @param toUserId   用户id
     * @return int
     */
    @Override
    public int msgRead(Integer fromUserId, Integer toUserId) {
        QueryWrapper<MsgInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_user_id",fromUserId);
        queryWrapper.eq("to_user_id",toUserId);
        MsgInfo msgInfo = new MsgInfo();
        msgInfo.setUnReadFlag(1);
        int update = msgInfoMapper.update(msgInfo, queryWrapper);
        return update;
    }
}
